package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Meeting;
import com.ruoyi.system.service.IMeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * meetingController
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@Controller
@RequestMapping("/system/meeting")
public class MeetingController extends BaseController
{
    @Autowired
    private IMeetingService meetingService;

    /**
     * 查询meeting列表
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(Meeting meeting)
    {
        startPage();
        List<Meeting> list = meetingService.selectMeetingList(meeting);
        return getDataTable(list);
    }

    /**
     * 导出meeting列表
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:export')")
    @Log(title = "meeting", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Meeting meeting)
    {
        List<Meeting> list = meetingService.selectMeetingList(meeting);
        ExcelUtil<Meeting> util = new ExcelUtil<Meeting>(Meeting.class);
        util.exportExcel(response, list, "meeting数据");
    }

    /**
     * 获取meeting详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(meetingService.selectMeetingById(id));
    }

    /**
     * 新增meeting
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:add')")
    @Log(title = "meeting", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Meeting meeting)
    {
        return toAjax(meetingService.insertMeeting(meeting));
    }

    /**
     * 修改meeting
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:edit')")
    @Log(title = "meeting", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Meeting meeting)
    {
        return toAjax(meetingService.updateMeeting(meeting));
    }

    /**
     * 删除meeting
     */
    @PreAuthorize("@ss.hasPermi('system:meeting:remove')")
    @Log(title = "meeting", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(meetingService.deleteMeetingByIds(ids));
    }

    @Log(title = "添加会议", businessType = BusinessType.INSERT)
    @PostMapping("/addcustom")
    @ResponseBody
    public AjaxResult addCustom(@Validated @RequestBody Meeting meeting)
    {
        LoginUser loginUser = getLoginUser();
        meeting.setUpdateBy(loginUser.getUsername());
       meeting.setId(UUID.randomUUID().toString());
       meeting.setDeleted("0");
       if (meeting.getNumber() == null){
            meeting.setNumber(new Long(meetingService.getMaxNumber() + 1));
       }
       meeting.setCreateTime(new Date());
       meeting.setUpdateTime(new Date());

        return toAjax(meetingService.insertMeeting(meeting));
    }

    @Log(title = "修改会议", businessType = BusinessType.UPDATE)
    @PostMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@Validated @RequestBody Meeting meeting)
    {
        String id = meeting.getId();
        Meeting oldmeeting = meetingService.selectMeetingById(id);
        meeting.setCreateTime(oldmeeting.getCreateTime());
        meeting.setUpdateTime(new Date());

        LoginUser loginUser = getLoginUser();
        meeting.setUpdateBy(loginUser.getUsername());
        return toAjax(meetingService.updateMeeting(meeting));
    }

    @Log(title = "删除会议", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("id") String id)
    {
        Meeting meeting = meetingService.selectMeetingById(id);

        meeting.setUpdateTime(new Date());
        meeting.setDeleted("1");

        return toAjax(meetingService.updateMeeting(meeting));
    }

    @GetMapping("/selectValid")
    @ResponseBody
    public Map<String, Object> selectValid(@RequestParam("conferencefield") String ppconferencefield, @RequestParam("conferencetitle") String ppconferencetitle, @RequestParam("conferenceaddress") String ppconferenceaddress,
                                  @RequestParam("meetingtime") String ppmeetingtime, @RequestParam("meetingendtime") String ppmeetingendtime, @RequestParam("zhuangtai") int ppzhuangtai,
                                  @RequestParam("paixu") String pppaixu, @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppconferencetitle)){
            ppconferencetitle = "%"+ppconferencetitle+"%";
        }

        System.out.println("zhuangtai:"+ppzhuangtai);

        int mmCount = meetingService.countValid(ppconferencefield, ppconferencetitle, ppconferenceaddress, ppmeetingtime, ppmeetingendtime, ppzhuangtai);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<Map<String,Object>>  mmInfo = meetingService.selectValid(ppconferencefield, ppconferencetitle, ppconferenceaddress, ppmeetingtime, ppmeetingendtime, ppzhuangtai, pppaixu, mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }

    @GetMapping("/selectall")
    @ResponseBody
    public Map<String, Object> selectall( @RequestParam("conferencetitle") String ppconferencetitle,  @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppconferencetitle)){
            ppconferencetitle = "%"+ppconferencetitle+"%";
        }

        int mmCount = meetingService.countAll(ppconferencetitle);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<Meeting> mmInfo = meetingService.selectAll( ppconferencetitle,  mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }

    @GetMapping("/selectmeetingaddress")
    @ResponseBody
    public List<Map<String, Object>> selectMeetingaddress()
    {
        return meetingService.selectMeetingaddress();
    }

    @GetMapping("/selectmeetingfield")
    @ResponseBody
    public List<Map<String, Object>> selectMeetingfield()
    {
        return meetingService.selectMeetingfield();
    }

    @GetMapping("/findone")
    @ResponseBody
    public Meeting findone(@RequestParam("id") String id)
    {
        Meeting meeting = meetingService.selectMeetingById(id);
        return meeting;
    }
}
